# 无标题窗口+圆角图片背景+全屏拖动+居中显示
# 1、一种是图片是方形图片，然后窗体进行裁剪成圆角
# 2、另一种是窗体以图片的形状进行圆角的裁剪（使用png图片作为遮罩）
# 本列示范第1种

import sys  
from PyQt5.QtWidgets import QApplication, QWidget  
from PyQt5.QtGui import QPixmap, QPainter, QPainterPath, QRegion  
from PyQt5.QtCore import Qt, QRect,QRectF
  
class RoundedWindow(QWidget):  
    def __init__(self):  
        super().__init__()  

        # 设置无边框  
        self.setWindowFlags(Qt.FramelessWindowHint)  
        # 设置窗口背景透明
        self.setAttribute(Qt.WA_TranslucentBackground)  
  
        # 加载圆角背景图片  
        self.background_pixmap = QPixmap('rect_back_2024.png')  
  
        # 设置窗体大小和圆角半径  
        pic_size = self.background_pixmap.size()
        self.setFixedSize(pic_size)  
        self.radius = 30.0  # 圆角半径，根据你的图片调整  
  
        # 触发绘制事件  
        self.update()  
  
    def paintEvent(self, event):  
        painter = QPainter(self)  
        painter.setRenderHint(QPainter.Antialiasing)  
  
        # 绘制圆角矩形路径  
        path = QPainterPath()  
        path.addRoundedRect(QRectF(0, 0, self.width(), self.height()), self.radius, self.radius)  
        # 设置裁剪区域为圆角矩形  
        painter.setClipPath(path)
  
        # 绘制背景图片  
        painter.drawPixmap(0, 0, self.background_pixmap)  
  
    # 这里还可以添加鼠标事件处理，以便用户可以拖动窗体  
    def mousePressEvent(self, event):  
        self.drag_position = event.globalPos() - self.frameGeometry().topLeft()  
  
    def mouseMoveEvent(self, event):  
        if event.buttons() == Qt.LeftButton:  
            self.move(event.globalPos() - self.drag_position)
  
if __name__ == '__main__':  
    app = QApplication(sys.argv)  
    window = RoundedWindow()  
    window.show()  
    sys.exit(app.exec_())
